﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.Data.SqlClient;

public partial class pax_list : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["pnr"] == null)
            {
                lblError.Text = "非法网页调用！";

                return;
            }
            lblOrderNo.Text = Request.QueryString["pnr"].ToString();
            if (Request.QueryString["is_new"].ToString() == "T")
            {
                MultiView1.ActiveViewIndex = 0;
                btnRemove.Visible = true;
            }
            else
            {
                MultiView1.ActiveViewIndex = -1;
                btnRemove.Visible = false;
            }
        }
        RetreivePaxList();
    }

    protected void RetreivePaxList()
    {
        CRSI.Order instOrder = new CRSI.Order();
        StringBuilder buf = new StringBuilder();
        int ret_code;
        string ret_msg;
        string age,inf_dob;
        DataSet dsNames;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsNames = instOrder.GetPaxList(Convert.ToInt64(lblOrderNo.Text), Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError2.Text = ret_msg;
            return;
        }
        if(ret_code>0)
        {
            switch(ret_code)
            {
                case 1:
                    lblError2.Text="订单找不到";
                    break;
                case 2:
                    lblError2.Text="无权查看";
                    break;
                case 3:
                    lblError2.Text="无查看本Office订单权限";
                    break;
                case 4:
                    lblError2.Text="无查看本Entity订单权限";
                    break;
                case 91:
                    lblError2.Text="登录错";
                    break;
                default:
                    lblError2.Text="未知错误："+ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        buf.Append("<table><tr><th></th><th>姓</th><th>名</th><th>性别</th><th>VIP</th><th>备注</th><th>年龄</th><th>生日</th><th>国籍</th><th>证件</th><th>证件号</th><th>证件到期日</th><th>电话</th><th>电子邮件</th><th>常客号码</th><th>邮寄行程单</th><th>邮寄地址</th></tr>");
        foreach (DataRow row in dsNames.Tables[0].Rows)
        {
            buf.Append("<tr><td><input type='radio' name='KEY' value='" + row["bk_id"].ToString() + "|" + row["pax_sn"].ToString() + "'></td><td>" + row["family_name"] + "/" + row["family_name_en"] + "</td><td>" + row["name"] + "/" + row["name_en"] + "</td>");
            if(Convert.ToBoolean(row["male"]))
                buf.Append("<td>男</td>");
            else
                buf.Append("<td>女</td>");
            if (Convert.ToBoolean(row["vip"]))
                buf.Append("<td>VIP</td>");
            else
                buf.Append("<td></td>");
            buf.Append("<td>" + row["remark"] + "</td>");
            age = row["age_type"].ToString();
            if ("A" == age)
                buf.Append("<td>成人</td>");
            else if ("C" == age)
                buf.Append("<td>儿童</td>");
            else if ("I" == age)
                buf.Append("<td>婴儿</td>");
            else
                buf.Append("<td>"+age+"</td>");
            if (Convert.IsDBNull(row["inf_dob"]))
                inf_dob = "";
            else
                inf_dob = Convert.ToDateTime(row["inf_dob"]).ToShortDateString();
            buf.Append("<td>" + inf_dob + "</td><td>" + row["nationality"] + "</td><td>" + row["id_type"] + "</td><td>" + row["id_no"] + "</td><td>" + Convert.ToDateTime(row["id_expired_date"]).ToShortDateString() + "</td><td>" + row["mobile_phone"] + "</td><td>" + row["email"] + "</td><td>" + row["ffp_airline"] + row["ffp_id"] + "</td>");
            if (Convert.ToBoolean(row["need_invoice"]))
                buf.Append("<td>是</td>");
            else
                buf.Append("<td>否</td>");
            buf.Append("<td>"+row["mail_address"]+"/"+row["post_code"]+"</td></tr>");

        }
        buf.Append("</table>");
        Literal1.Text = buf.ToString();

    }
    //protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    //{
    //    //e.InputParameters["searchMethod"] = this.searchMethod;
    //    e.InputParameters["user"] = Application["UID"].ToString();
    //    e.InputParameters["password"] = Application["PWD"].ToString();

    //}
    //protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    //{
    //    int ret_code = Convert.ToInt32(e.OutputParameters["ret_code"]);
    //    string ret_msg = e.OutputParameters["ret_msg"].ToString();
    //    if (ret_code < 0)
    //    {
    //        lblError.Text = ret_msg;
    //        return;
    //    }
    //    switch(ret_code)
    //    {
    //        case 0:
    //            lblError.Text="";
    //            GridView1.DataSource = ((DataSet)(e.ReturnValue)).Tables[0];
    //            GridView1.DataBind();
    //            break;
    //        case 1:
    //            lblError.Text="找不到订单！";
    //            break;
    //        case 2:
    //            lblError.Text = "无权查看订单！";
    //            break;
    //        case 3:
    //            lblError.Text="无查看本Office订单权限!";
    //            break;
    //        case 4:
    //            lblError.Text="无查看本Entity订单权限!";
    //            break;
    //        case 5:
    //            lblError.Text="登录失败!";
    //            break;
    //        default:
    //            lblError.Text="未知错误："+ret_code.ToString();
    //            break;
    //    }

    //}
    protected void btnAddPax_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code=0;
        string ret_msg;
        bool male,invoice;
        if(radMale.Checked)
            male=true;
        else if (radFemale.Checked)
            male = false;
        else
        {
            lblError.Text = "请选择性别！";
            return;
        }
        if(lstAgeType.SelectedValue=="I" && txtInfDob.Text=="")
        {
            lblError.Text = "婴儿旅客需输入出生日期！";
            return;
        }
        if (chkHoldInf.Checked && (lstAgeType.SelectedValue == "C" || lstAgeType.SelectedValue == "I"))
        {
            lblError.Text = "A child or infant passenger can not hold infant!";
            return;
        }
        if (radUnNeed.Checked)
            invoice = false;
        else
            invoice = true;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + txtIDNo.Text + "|" + Application["HASH_PWD"].ToString(), "MD5");
        try
        {
            ret_code = instOrder.AddPax(Convert.ToInt64(lblOrderNo.Text), "", "",-1, txtFamilyNameEn.Text, txtNameEn.Text, txtFamilyName.Text, txtName.Text,chkHoldInf.Checked,male, Convert.ToChar(lstAgeType.SelectedValue),
                txtInfDob.Text, lstCountries.SelectedValue, lstIDType.SelectedValue, txtIDNo.Text, Convert.ToDateTime(txtIDExpiry.Text), txtPhone.Text, txtEmail.Text,
                chkVIP.Checked,chkAloneChd.Checked,lstMoveAssistant.SelectedValue,lstMeal.SelectedValue,txtRemark.Text,invoice,txtAddress.Text,txtPostCode.Text, Application["UID"].ToString(), sign, out ret_msg);
            if (ret_code < 0)
            {
                lblError.Text = ret_msg;
                return;
            }
            switch (ret_code)
            {
                case 0:
                    lblError.Text = "";
                    //radUnNeed.Checked = true;
                    //txtAddress.Text = "";
                    //txtPostCode.Text = "";
                    RetreivePaxList();
                    break;
                case 1:
                    lblError.Text = "订单找不到";
                    break;
                case 2:
                    lblError.Text = "更新旅客序号失败";
                    break;
                case 3:
                    lblError.Text = "旅客名单已齐，不能新增";
                    break;
                case 4:
                    lblError.Text = "订单状态不允许新增";
                    break;
                case 5:
                    lblError.Text = "旅客名单已齐，不能新增";
                    break;
                case 6:
                    lblError.Text = "旅客数错";
                    break;
                case 7:
                    lblError.Text = "添加失败";
                    break;
                case 8:
                    lblError.Text = "儿童/婴儿旅客不能再抱婴儿";
                    break;
                case 9:
                    lblError.Text = "年龄代码错";
                    break;
                //case 10:
                //    lblError.Text = "婴儿仅可选择婴儿餐、流质餐、任意餐、无餐食";
                //    break;
                case 11:
                    lblError.Text = "非儿童旅客不属于'无人陪伴儿童'!";
                    break;
                case 12:
                    lblError.Text = "婴儿旅客需输入出生日期!";
                    break;
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }

        


    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        if (!chkConfirm.Checked)
        {
            lblError.Text = "请确认名单已正确，并在下面打钩确认。";
            return;
        }
        try
        {
            //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
            string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
            ret_code = instOrder.Submit(Convert.ToInt64(lblOrderNo.Text), Application["UID"].ToString(), sign, out ret_msg);
            if (ret_code < 0)
            {
                lblError.Text = ret_msg;
                return;
            }
            switch (ret_code)
            {
                case 0:
                    ////update local order status?
                    //SqlCommand cmd = new SqlCommand();
                    //cmd.CommandText = "save_local_order";
                    //SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
                    //para = cmd.Parameters.AddWithValue("@order_no", lblOrderNo.Text);
                    //para = cmd.Parameters.AddWithValue("@contact", txtContactMan.Text);
                    //para = cmd.Parameters.AddWithValue("@mobile_phone", txtMobilePhone.Text);
                    //para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                    //ret_code = TinyDAL.Exec(ref cmd);
                    //if (ret_code != 0)
                    //{
                    //    lblError.Text = "保存订单出错！请手工记录订单号";//用户看不见！
                    //    return;
                    //}
                    //RetreivePaxList();
                    
                    //Response.Redirect("pay_order.aspx?pnr=" + lblOrderNo.Text);
                    Response.Redirect("order.aspx?pnr=" + lblOrderNo.Text);
                    break;
                case 1:
                    lblError.Text = "订单找不到";
                    break;
                case 2:
                    lblError.Text = "更新状态失败";
                    break;
                case 3:
                    lblError.Text = "旅客名单不齐";
                    break;
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        RetreivePaxList();
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if(Request.Form["KEY"]==null)
        {
            lblError.Text="请选择要删除的旅客";
            return;
        }
        string key;
        string[] strArray;
        key = Request.Form["KEY"].ToString();
        strArray = key.Split('|');
        int pax_sn;
        try
        {
            pax_sn = Convert.ToInt16(strArray[1]);
        }
        catch(Exception ex)
        {
            lblError.Text="非法旅客序号！";
            return;
        }
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Convert.ToInt16(pax_sn).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        ret_code = instOrder.RemovePax(Convert.ToInt64(strArray[0]), pax_sn, Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        switch (ret_code)
        {
            case 0:
                RetreivePaxList();
                lblError.Text = "";
                break;
            case 1:
                lblError.Text = "找不到订单";
                break;
            case 2:
                lblError.Text = "订单状态不允许修改旅客名单";
                break;
            case 3:
                lblError.Text = "取消订单失败3";
                break;
            case 4:
                lblError.Text = "取消订单失败4";
                break;
            case 8:
                lblError.Text = "无权限";
                break;
            case 91:
                lblError.Text = "登录错";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                break;
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(lblOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        ret_code = instOrder.Cancel(Convert.ToInt64(lblOrderNo.Text), Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        switch (ret_code)
        {
            case 0:
                //update local order status
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "cancel_order";
                SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
                para = cmd.Parameters.AddWithValue("@order_no", lblOrderNo.Text);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code != 0)
                {
                    lblError.Text = "本地订单删除失败，实际订单已取消。";
                    return;
                }
                ////RetreivePaxList();
                lblError.Text = "订单已取消。";
                break;
            case 1:
                lblError.Text = "取消失败：1";
                break;
            case 2:
                lblError.Text = "取消失败：2";
                break;
            case 3:
                lblError.Text = "取消失败：3";
                break;
            case 4:
                lblError.Text = "取消失败：4";
                break;
            case 5:
                lblError.Text = "找不到订单";
                break;
            case 6:
                lblError.Text = "无权限";
                break;
            case 81:
                lblError.Text = "取消失败：81";
                break;
            case 90:
                lblError.Text = "取消失败：90";
                break;
            case 91:
                lblError.Text = "登录错";
                break;
            case 150:
                lblError.Text = "取消失败：150";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                break;
        }

        
    }
    protected void btnRefresh_Click1(object sender, EventArgs e)
    {

    }
}
